package com.fly.controller;

import com.fly.constants.StatusConstants;
import com.fly.repository.impl.InMemoryChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class AiServiceChatController {

    private final ChatClient serviceChatClient;

    private final InMemoryChatHistoryRepository chatHistoryRepository;

    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> service(String prompt, @RequestParam("chatId") String chatId) {
        // 存储会话id
        chatHistoryRepository.save(StatusConstants.TYPE_SERVICE, chatId);
        // 返回大模型的内容
        return serviceChatClient
                .prompt(prompt)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream()
                .content();
    }

}
